package com.gx.util.base;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author gaoxing
 * @create 2018-05-15 下午2:08
 */
public class CommonDateUtils {
    private static final FastDateFormat DATE_FORMAT;
    public static final TimeZone CHINA_TIMEZONE = TimeZone.getTimeZone("GMT+8");

    public static void main(String[] args) {
//        System.out.println(getNextMonthStart(new Date()));
//        System.out.println(getMonthStart(new Date()));

        System.out.println("~~~~~~~~");

        System.out.println(LocalDate.now());//2018-05-15
        System.out.println(LocalDate.now().atStartOfDay());//2018-05-15T00:00
        System.out.println(LocalDate.now().lengthOfMonth());//31
        System.out.println(LocalDate.now().getChronology());
        System.out.println(LocalDate.now().getMonth());//MAY
        System.out.println(LocalDate.now().getMonthValue());//5
        System.out.println(LocalDate.now().getDayOfMonth());//15
        System.out.println(LocalDate.now().getDayOfWeek());//TUESDAY
        System.out.println(LocalDate.now().getDayOfWeek().getValue());//TUESDAY
        System.out.println(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()));//TUESDAY
        System.out.println(LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth()));//TUESDAY
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()));//TUESDAY
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastDayOfYear()));//TUESDAY
        System.out.println(LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));//TUESDAY
        System.out.println(LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));//TUESDAY

        System.out.println("~~~~~~~~");
        System.out.println(LocalDateTime.now());//2018-05-15T14:37:41.141
        System.out.println(LocalDateTime.now().getYear());//2018
        System.out.println(LocalDateTime.now().getMonth());//MAY
        System.out.println(LocalDateTime.now().getMonthValue());//5
        System.out.println(LocalDateTime.now().getDayOfMonth());//15
        System.out.println(LocalDateTime.now().getDayOfYear());//135
        System.out.println(LocalDateTime.now().getDayOfWeek());//TUESDAY
        System.out.println(LocalDateTime.now().getDayOfWeek().getValue());//2
        System.out.println(LocalDateTime.now().getHour());//14
        System.out.println(LocalDateTime.now().getMinute());//41
        System.out.println(LocalDateTime.now().getSecond());//10
        System.out.println(LocalDateTime.now().getNano());//949000000

        System.out.println("~~~~~~~~~~~~~~~~~");
//        FastDateFormat a = FastDateFormat.getInstance("yyyy-MM-dd", TimeZone.getTimeZone("GMT+8"));
//        FastDateFormat b = FastDateFormat.getInstance("yyyy-MM-dd", TimeZone.getTimeZone("GMT+19"));
//        FastDateFormat c = FastDateFormat.getInstance("yyyy-MM-dd", TimeZone.getTimeZone("GMT+20"));
//        FastDateFormat d = FastDateFormat.getInstance("yyyy-MM-dd", TimeZone.getTimeZone("GMT+21"));
//        System.out.println(a.format(new Date()));
//        System.out.println(b.format(new Date()));
//        System.out.println(c.format(new Date()));
//        System.out.println(d.format(new Date()));
    }
    public static Date getMonthStart(Date date) {
        Preconditions.checkArgument(date != null, "时间对象不能为空");
        String chinaDate = toDateFormat(date);
        chinaDate = chinaDate.substring(0, 8) + "01";
        return fromDateFormat(chinaDate);
    }

    public static Date getNextMonthStart(Date date) {
        Preconditions.checkArgument(date != null, "时间对象不能为空");
        String chinaDate = toDateFormat(date);
        int month = Integer.parseInt(chinaDate.substring(5, 7)) + 1;
        String sMonth = month + "";
        if (month < 10) {
            sMonth = "0" + sMonth;
        }

        return fromDateFormat(chinaDate.substring(0, 5) + sMonth + "-01");
    }
    public static String toDateFormat(Date date) {
        Preconditions.checkArgument(date != null, "时间对象不能为空");
        return DATE_FORMAT.format(date);
    }
    public static Date fromDateFormat(String s) {
        if (StringUtils.isBlank(s)) {
            return null;
        } else {
            try {
                return DATE_FORMAT.parse(s);
            } catch (Exception var2) {
                throw new IllegalArgumentException("格式不符合yyyy-MM-dd");
            }
        }


    }
    static {
        DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd", CHINA_TIMEZONE);
//        TIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss", CHINA_TIMEZONE);
//        STANDARD_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZ", CHINA_TIMEZONE);
//        defaultFormats = ImmutableMap.builder().put("yyyy-MM-dd", DATE_FORMAT).put("yyyy-MM-dd HH:mm:ss", TIME_FORMAT).put("yyyy-MM-dd'T'HH:mm:ss.SSSZ", STANDARD_FORMAT).put("yyyyMMdd", FastDateFormat.getInstance("yyyyMMdd", CHINA_TIMEZONE)).put("yyyyMMddHHmmss", FastDateFormat.getInstance("yyyyMMddHHmmss", CHINA_TIMEZONE)).put("yyyyMMddHHmmssSSS", FastDateFormat.getInstance("yyyyMMddHHmmssSSS", CHINA_TIMEZONE)).put("yyyy-M-dd", FastDateFormat.getInstance("yyyy-M-dd", CHINA_TIMEZONE)).put("yyyy-MM", FastDateFormat.getInstance("yyyy-MM", CHINA_TIMEZONE)).put("yyyy-M", FastDateFormat.getInstance("yyyy-M", CHINA_TIMEZONE)).put("yyyyMddHHmmss", FastDateFormat.getInstance("yyyyMddHHmmss", CHINA_TIMEZONE)).put("yyyyMM", FastDateFormat.getInstance("yyyyMM", CHINA_TIMEZONE)).put("yyyyM", FastDateFormat.getInstance("yyyyM", CHINA_TIMEZONE)).put("yyyy/MM/dd", FastDateFormat.getInstance("yyyy/MM/dd", CHINA_TIMEZONE)).put("yyyy/MM/dd HH:mm:ss", FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss", CHINA_TIMEZONE)).put("yyyy/MM/dd'T'HH:mm:ss.SSSZ", FastDateFormat.getInstance("yyyy/MM/dd'T'HH:mm:ss.SSSZ", CHINA_TIMEZONE)).put("yyyy/M/dd", FastDateFormat.getInstance("yyyy/M/dd", CHINA_TIMEZONE)).put("yyyy/MM", FastDateFormat.getInstance("yyyy/MM", CHINA_TIMEZONE)).put("yyyy/M", FastDateFormat.getInstance("yyyy/M", CHINA_TIMEZONE)).build();
//        defaultPatterns = ImmutableMap.builder().put(Pattern.compile("\\d{4}-\\d{2}-\\d{2}"), DATE_FORMAT).put(Pattern.compile("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}"), TIME_FORMAT).put(Pattern.compile("\\d{4}-\\d{2}-\\d{2}'T'\\d{2}:\\d{2}:\\d{2}.\\d{3}.*"), STANDARD_FORMAT).put(Pattern.compile("\\d{8}"), defaultFormats.get("yyyyMMdd")).put(Pattern.compile("\\d{14}"), defaultFormats.get("yyyyMMddHHmmss")).put(Pattern.compile("\\d{17}"), defaultFormats.get("yyyyMMddHHmmssSSS")).put(Pattern.compile("\\d{4}-\\d{1,2}-\\d{2}"), defaultFormats.get("yyyy-M-dd")).put(Pattern.compile("\\d{4}-\\d{2}"), defaultFormats.get("yyyy-MM")).put(Pattern.compile("\\d{4}-\\d{1,2}"), defaultFormats.get("yyyy-M")).put(Pattern.compile("\\d{13,14}"), defaultFormats.get("yyyyMddHHmmss")).put(Pattern.compile("\\d{6}"), defaultFormats.get("yyyyMM")).put(Pattern.compile("\\d{5,6}"), defaultFormats.get("yyyyM")).put(Pattern.compile("\\d{4}/\\d{2}/\\d{2}"), defaultFormats.get("yyyy/MM/dd")).put(Pattern.compile("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}"), defaultFormats.get("yyyy/MM/dd HH:mm:ss")).put(Pattern.compile("\\d{4}/\\d{2}/\\d{2}'T'\\d{2}:\\d{2}:\\d{2}.\\d{3}.*"), defaultFormats.get("yyyy/MM/dd'T'HH:mm:ss.SSSZ")).put(Pattern.compile("\\d{4}/\\d{1,2}/\\d{2}"), defaultFormats.get("yyyy/M/dd")).put(Pattern.compile("\\d{4}/\\d{2}"), defaultFormats.get("yyyy/MM")).put(Pattern.compile("\\d{4}/\\d{1,2}"), defaultFormats.get("yyyy/M")).build();
//        millsPerDay = 86400000L;
//        TIME_PATTERN = Pattern.compile("(-?\\d+)(y|M|d|h|m|s|ms)");
//        TIMES_PATTERN = Pattern.compile("((-?\\d+)(y|M|d|h|m|s|ms))+");
    }

}
